//
//  HappyGivenTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/2.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HappyGivenTableViewCell.h"
#import "HappyGivenCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfHappyGiven.h"
@interface HappyGivenTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfHappyGiven;
@property (nonatomic, strong) NSMutableArray *arrayOfSection;

@end
@implementation HappyGivenTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[HappyGivenCollectionViewCell class] forCellWithReuseIdentifier:@"HappyGivenCollectionViewCell"];
    }
    return self;
}

- (void)handleData
{
    _arrayOfHappyGiven = [NSMutableArray array];
    _arrayOfSection = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=happy_send&op=index&curpage=1&page=5" par:nil success:^(id responseObject) {
        NSArray *array = responseObject[@"datas"];
        for (NSDictionary *dic in array) {
            ModelOfHappyGiven *model = [[ModelOfHappyGiven alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfHappyGiven addObject:model];
        }
        [_arrayOfSection addObject:[_arrayOfHappyGiven lastObject]];
        [_arrayOfHappyGiven removeLastObject];
        //协议传值-传回VC 给section的头视图赋值
        if ([self.delegate respondsToSelector:@selector(giveSectionArray:)] && self.delegate != nil) {
            [self.delegate giveSectionArray:_arrayOfSection];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 30) / 2, (self.contentView.bounds.size.height - 30) / 2);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfHappyGiven.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HappyGivenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HappyGivenCollectionViewCell" forIndexPath:indexPath];
    ModelOfHappyGiven *model = [_arrayOfHappyGiven objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.goods_image]];
    if (model.goods_name.length < 8) {
        cell.labelOfName.text = model.goods_name;
    } else {
        cell.labelOfName.text = [model.goods_name substringFromIndex:8];
    }
    cell.labelOfContent.text = [NSString stringWithFormat:@"¥ %@", model.goods_price];
    cell.labelOfName.textAlignment = 1;
    cell.labelOfContent.textAlignment = 1;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfHappyGiven *model = [_arrayOfHappyGiven objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(happyGivenWithGood_id:)]) {
        [self.delegate happyGivenWithGood_id:model.goods_id];
    }
}
@end
